﻿namespace USElections.Utils
{
    using System;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using Microsoft.Phone.Controls;

    /// <summary>
    /// Animations class.
    /// </summary>
    public class Animations
    {
        /// <summary>
        /// Opening animation.
        /// </summary>
        /// <param name="sender">The LongListSelector object.</param>
        /// <param name="e">The GroupViewOpenedEventArgs object.</param>
        public static void LongListSelectorOpen(LongListSelector sender, GroupViewOpenedEventArgs e)
        {
            // Construct and begin a swivel animation to pop in the group view.
            IEasingFunction quadraticEase = new QuadraticEase { EasingMode = EasingMode.EaseOut };

            if (e != null)
            {
                ItemsControl groupItems = e.ItemsControl;

                if (groupItems != null)
                {
                    foreach (var item in groupItems.Items)
                    {
                        UIElement container = groupItems.ItemContainerGenerator.ContainerFromItem(item) as UIElement;

                        if (container != null)
                        {
                            Border content = VisualTreeHelper.GetChild(container, 0) as Border;

                            if (content != null)
                            {
                                DoubleAnimationUsingKeyFrames showAnimation = new DoubleAnimationUsingKeyFrames();
                                ////showAnimation.RepeatBehavior = RepeatBehavior.Forever;

                                EasingDoubleKeyFrame showKeyFrame1 = new EasingDoubleKeyFrame();
                                showKeyFrame1.KeyTime = TimeSpan.FromMilliseconds(0);
                                showKeyFrame1.Value = -60;
                                showKeyFrame1.EasingFunction = quadraticEase;

                                EasingDoubleKeyFrame showKeyFrame2 = new EasingDoubleKeyFrame();
                                showKeyFrame2.KeyTime = TimeSpan.FromMilliseconds(85);
                                showKeyFrame2.Value = 0;
                                showKeyFrame2.EasingFunction = quadraticEase;

                                showAnimation.KeyFrames.Add(showKeyFrame1);
                                showAnimation.KeyFrames.Add(showKeyFrame2);

                                Storyboard.SetTargetProperty(showAnimation, new PropertyPath(PlaneProjection.RotationXProperty));

                                if (content.Projection != null)
                                {
                                    Storyboard.SetTarget(showAnimation, content.Projection);
                                    Storyboard swivelShow = new Storyboard();
                                    swivelShow.Children.Add(showAnimation);
                                    swivelShow.Begin();
                                }
                            }
                        }
                    }
                }
            }
        }

        /// <summary>
        /// Closing animation.
        /// </summary>
        /// <param name="sender">The LongListSelector object.</param>
        /// <param name="e">The GroupViewClosingEventArgs object.</param>
        public static void LongListSelectorClosing(LongListSelector sender, GroupViewClosingEventArgs e)
        {
            // Cancelling automatic closing and scrolling to do it manually.
            e.Cancel = true;

            if (e.SelectedGroup != null)
            {
                sender.ScrollToGroup(e.SelectedGroup);
            }

            Deployment.Current.Dispatcher.BeginInvoke(() =>
                {
                    // Construct and begin a swivel animation to pop out the group view.
                    IEasingFunction quadraticEase = new QuadraticEase { EasingMode = EasingMode.EaseOut };

                    if (e != null)
                    {
                        ItemsControl groupItems = e.ItemsControl;

                        if (groupItems != null)
                        {
                            foreach (var item in groupItems.Items)
                            {
                                UIElement container = groupItems.ItemContainerGenerator.ContainerFromItem(item) as UIElement;
                                if (container != null)
                                {
                                    Border content = VisualTreeHelper.GetChild(container, 0) as Border;
                                    if (content != null)
                                    {
                                        DoubleAnimationUsingKeyFrames showAnimation = new DoubleAnimationUsingKeyFrames();

                                        EasingDoubleKeyFrame showKeyFrame1 = new EasingDoubleKeyFrame();
                                        showKeyFrame1.KeyTime = TimeSpan.FromMilliseconds(0);
                                        showKeyFrame1.Value = 0;
                                        showKeyFrame1.EasingFunction = quadraticEase;

                                        EasingDoubleKeyFrame showKeyFrame2 = new EasingDoubleKeyFrame();
                                        showKeyFrame2.KeyTime = TimeSpan.FromMilliseconds(125);
                                        showKeyFrame2.Value = 90;
                                        showKeyFrame2.EasingFunction = quadraticEase;

                                        showAnimation.KeyFrames.Add(showKeyFrame1);
                                        showAnimation.KeyFrames.Add(showKeyFrame2);

                                        Storyboard.SetTargetProperty(showAnimation, new PropertyPath(PlaneProjection.RotationXProperty));
                                        Storyboard.SetTarget(showAnimation, content.Projection);

                                        Storyboard swivelHide = new Storyboard();
                                        swivelHide.Children.Add(showAnimation);

                                        swivelHide.Completed += (sender1, e1) =>
                                        {
                                            sender.CloseGroupView();
                                        };

                                        swivelHide.Begin();
                                    }
                                }
                            }
                        }
                    }
                });
        }
    }
}
